﻿
/// <summary>
/// 本地存储助手
/// </summary>
public static class LocalStoreHelper
{
	/// <summary>
	/// 存储
	/// </summary>
	/// <param name="origin">对象</param>
	/// <param name="path">路径</param>
	/// <param name="objectType">对象类型</param>
	public static void Store(object origin, string path, ObjectType objectType = ObjectType.Struct)
	{
		byte[] buff = null;

		switch (objectType)
		{
		case ObjectType.Struct:
			buff = ByteConvertHelper.StructToBytes(origin);
			break;
		case ObjectType.Object:
			buff = ByteConvertHelper.ObjectToBytes(origin);
			break;
		}

		FileConvertHelper.BytesToFile(buff, path);
	}

	/// <summary>
	/// 获取
	/// </summary>
	/// <param name="path">路径</param>
	/// <param name="objectType">对象类型</param>
	/// <typeparam name="T">类型</typeparam>
	public static T Obtain<T>(string path, ObjectType objectType = ObjectType.Struct)
	{
		byte[] buff = FileConvertHelper.FileToBytes(path);

		switch (objectType)
		{
		case ObjectType.Struct:
			return (T)ByteConvertHelper.BytesToStruct(buff, typeof(T));
		case ObjectType.Object:
			return (T)ByteConvertHelper.BytesToObject(buff);
		}

		return default(T);
	}

	/// <summary>
	/// 对象类型
	/// </summary>
	public enum ObjectType
	{
		/// <summary>
		/// 结构体
		/// </summary>
		Struct,

		/// <summary>
		/// 对象
		/// </summary>
		Object
	}
}
